//
//  WZRootViewController.m
//  ETong
//
//  Created by wanzhao on 15/12/17.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZRootViewController.h"
#import "WZTabBarController.h"
#import "WZNavigationController.h"
#import "WZMenuViewController.h"

@interface WZRootViewController ()<UIGestureRecognizerDelegate, WZMenuViewControllerDelegate>

@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, assign) CGPoint ptouch;
@property (nonatomic, assign) CGPoint touch;

@property (nonatomic, weak) UIView *coverView;

@property (nonatomic, weak) UITapGestureRecognizer *tap;
@property (nonatomic, weak) UIPanGestureRecognizer *pan;
@property (nonatomic, weak) UISwipeGestureRecognizer *swipLeft;
@property (nonatomic ,weak) UISwipeGestureRecognizer *swipRight;

@end

@implementation WZRootViewController


- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.view.backgroundColor = WhiteColor;
   
    
    // leftView
    self.leftView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.leftView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.leftView];
    // mainView
    self.mainView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.mainView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.mainView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeGuesture) name:REMOVEGUESTURE object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addGuesture) name:ADDGUESTURE object:nil];
    
    
#pragma mark -- 创建TabBar控制器
//    WZTabBarController *tab = [[WZTabBarController alloc] init];
    WZTabBarController *tab = [[WZTabBarController alloc] init];
    [self addChildViewController:tab];
    [self.mainView addSubview:tab.view];
    
    UIView *coverView = [[UIView alloc] initWithFrame:self.view.bounds];
    coverView.hidden = YES;
    coverView.backgroundColor = ClearColor;
    [self.mainView addSubview:coverView];
    self.coverView = coverView;
    
    WZMenuViewController *menu = [[WZMenuViewController alloc] init];
    menu.menuControllerDelegate = self;
    [self addChildViewController:menu];
    [self.leftView addSubview:menu.view];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = YES;
    [self addGuesture];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self removeGuesture];
}
- (void)addGuesture
{
    if (self.tap == nil) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(restoreLocation)];
        [self.coverView addGestureRecognizer:tap];
        self.tap = tap;
    }
    
    if (self.pan == nil) {
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
        [self.view addGestureRecognizer:pan];
        self.pan = pan;
 
    }
    
    if (self.swipRight == nil) {
        UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
        swip.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:swip];
        self.swipRight = swip;

    }
    
    if (self.swipLeft == nil) {
        UISwipeGestureRecognizer *swipLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(restoreLocation)];
        swipLeft.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:swipLeft];
        self.swipLeft = swipLeft;
 
    }
    
    [self.pan requireGestureRecognizerToFail:self.swipLeft];
    [self.pan requireGestureRecognizerToFail:self.swipRight];

}

- (void)removeGuesture
{
    [self.coverView removeGestureRecognizer:self.tap];
    [self.tap removeTarget:self action:@selector(restoreLocation)];
    self.tap = nil;
    
    [self.view removeGestureRecognizer:self.swipLeft];
    [self.swipLeft removeTarget:self action:@selector(restoreLocation)];
    self.swipLeft = nil;
    
    [self.view removeGestureRecognizer:self.swipRight];
    [self.swipRight removeTarget:self action:@selector(showMenu:)];
    self.swipRight = nil;
    
    [self.view removeGestureRecognizer:self.pan];
    [self.pan removeTarget:self action:@selector(showMenu:)];
    self.pan = nil;
}
- (void)showMenu:(UIGestureRecognizer *)recognizer
{
    
    //之前触摸点
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        self.ptouch = [recognizer locationInView:self.view];
    } else {
        self.ptouch = self.touch;
    }
    //当前触摸点
   self.touch = [recognizer locationInView:self.view];
    
    if ([recognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
        self.coverView.hidden = NO;
        [UIView animateWithDuration:0.25f animations:^{
            self.mainView.x = OppositeWidthRate(60);
        } completion:^(BOOL finished) {
            
        }];
    } else if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]){
        self.mainView.x = self.mainView.x + self.touch.x - self.ptouch.x;
        
        if (self.mainView.x > 60.0f) {
            self.mainView.x = 60.0f;
        } else if (self.mainView.x < 0) {
            self.mainView.x = 0;
        }
        if (recognizer.state == UIGestureRecognizerStateEnded) {
            if (self.mainView.x >= 30.0f) {
                self.mainView.x = 60.0f;
                self.coverView.hidden = NO;
            } else {
                self.mainView.x = 0;
                self.coverView.hidden = YES;
            }
        }
    }
}


- (void)menuViewControllerDidSelectedWithClass:(Class)destVc
{
    self.mainView.x = 0;
    self.coverView.hidden = YES;
    UIViewController *vc = [[destVc alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

}
// 恢复位置
- (void)restoreLocation
{
    self.coverView.hidden = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.mainView.x = 0;
    } completion:^(BOOL finished) {
        
    }];
}

@end
